package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.dao.EngineKeywordDao;
import com.yeyaomai.dksns.flowis.dao.KeywordDao;
import com.yeyaomai.dksns.flowis.domain.EngineKeyword;
import com.yeyaomai.dksns.flowis.domain.Keyword;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.KeywordService;

@Service
public class KeywordServiceImpl implements KeywordService {

	@Resource
	private KeywordDao keywordDao;
	
	@Resource
	private EngineKeywordDao engineKeywordDao;
	
	@Resource
	private DetailDao detailDao;
	
	@Override
	public void addKeyword(Keyword keyword) {

		keywordDao.addKeyword(keyword);
	}

	@Override
	public List<Keyword> getKeywordByDate(String st ,String et) {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("st", st);
		map.put("et", et);
		return keywordDao.getKeywordByDate(map);
	}

	@Override
	public List<EngineKeyword> getEngineKeywordByKeywordDate(String keyword, String st ,String et) {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("keyword", keyword);
		map.put("st", st);
		map.put("et", et);
		return engineKeywordDao.getEngineKeywordByKeywordDate(map);
	}
	
	@Override
	public Page<Keyword> getSplit(FlowisQueryOrder order) {
		
		PageRequestOrder page = new PageRequestOrder(Integer.valueOf(order.getNumber()) , SplitConstant.PAGE_SIZE);
		List<Keyword> keywords = getKeywordByDate(order.getStartTime(), order.getEndTime());
		List<Keyword> kws =	interception(keywords, page.start() ,page.end());
		Page<Keyword> result = new PageImplResult<Keyword>(kws, page, keywords.size());
		return result;
	}
	
	public List<Keyword> interception(List<Keyword> keywords ,Integer start , Integer ent){
		
		List<Keyword> list = new ArrayList<Keyword>();
		if(start>=0){
			ent =  ent>keywords.size() ? keywords.size() : ent ;
			for(int i = start; i< ent; i++ ){
				list.add(keywords.get(i));
			}
		}
		return list;
	}
	
	@Override
	public Page<FlowisStatDetailDTO> getSplitLately(FlowisQueryOrder order){
		
		List<String> list = new ArrayList<String>();
		list.add("null");
		list.add("");
		Query query = new Query(Criteria.where("access_date").gte(order.getStartTime()).lte(order.getEndTime())
				.and("referer").ne("duankou").and("keyword").nin(list).and("engineName").nin(list));
		
		PageRequestOrder request = new PageRequestOrder(Integer.parseInt(order
				.getNumber()), order.getPageSize());

		query.sort().on("access_date", Order.DESCENDING);
		List<FlowisStatDetailDTO> flowisList = detailDao.findList(query);
		long totalSize =flowisList.size()>500 ? 500 : flowisList.size();
		
		query.skip(request.start()).limit(order.getPageSize());

		List<FlowisStatDetailDTO> flowList = detailDao.findList(query);

		Page<FlowisStatDetailDTO> result = new PageImplResult<FlowisStatDetailDTO>(flowList, request, totalSize);
		
		return result;
	}
	
	@Override
	public List<FlowisStatDetailDTO> getSearchLately(String st ,String et){
		
		List<String> list = new ArrayList<String>();
		list.add("null");
		list.add("");
		Query query = new Query(Criteria.where("access_date").gte(st).lte(et)
				.and("referer").ne("duankou").and("keyword").nin(list).and("engineName").nin(list));

		query.sort().on("access_date", Order.DESCENDING);
		List<FlowisStatDetailDTO> flowisList = detailDao.findList(query);
		return flowisList;
	}
	
}
